package com.fausto.learnimprint.core.controller.impl

import com.fausto.learnimprint.core.model.dto.CommonResult
import com.fausto.learnimprint.core.model.entity.Reply
import com.fausto.learnimprint.core.service.IReplyService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 *
 *
 * 前端控制器
 *
 *
 * @author fausto
 * @since 2022-03-28
 */
@Api(tags = ["回复API"], basePath = "/replys")
@RestController
@RequestMapping("/replys")
class ReplyController{

    @Autowired
    lateinit var replyService: IReplyService

    /**
     *      发布新的回复
     */
    @ApiOperation("发布回复")
    @PreAuthorize("hasAuthority('USER')")
    @PostMapping("/{discussId}")
    fun newReply(
        @ApiParam(value = "回复的评论ID", example = "1")
        @PathVariable discussId: Int,
        @ApiParam(value = "回复内容", example = "回复内容测试")
        @RequestBody replyBody: String,
    ): CommonResult<Any>{
        return if (replyBody.isBlank()){
            CommonResult.fail("参数异常")
        }else try {
            if (replyService.saveReply(discussId, replyBody)){
                CommonResult.ok("回复成功")
            }else{
                CommonResult.fail("回复失败")
            }
        }catch (e: Exception){
            CommonResult.fail("回复失败", data = e.message)
        }
    }

    @ApiOperation("删除回复")
    @PreAuthorize("hasAuthority('USER')")
    @DeleteMapping("/{replyId}")
    fun deleteReply(
        @ApiParam(value = "回复ID", example = "1")
        @PathVariable replyId: Int
    ): CommonResult<Any>{
        return if (replyId <= 0) {
             CommonResult.fail("参数错误")
        }else try {
            if (replyService.deleteReply(replyId)){
                CommonResult.ok("删除失败")
            }else{
               CommonResult.fail("删除失败")
            }
        }catch (e: Exception){
            CommonResult.fail("删除失败", data = e.message)
        }
    }

    @ApiOperation("管理员删除回复")
    @PreAuthorize("hasAuthority('ADMIN')")
    @DeleteMapping("/admin/{replyId}")
    fun adminDelete(
        @ApiParam(value = "回复ID", example = "1")
        @PathVariable replyId: Int
    ): CommonResult<Any>{
        return if (replyId < 1){
            CommonResult.fail("参数错误")
        }else try {
            if (replyService.adminDelete(replyId)){
                CommonResult.ok("删除成功")
            }else{
                CommonResult.fail("删除失败")
            }
        }catch (e: Exception){
            CommonResult.fail("删除失败", data = e.message)
        }
    }
}